﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using fideeraSoft.Utils;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing;

namespace fideeraSoft.Paneles
{
    public class ConsultaStockActualPorMateriaPrimaPanel:PanelGeneral
    {
        private System.Windows.Forms.DataGridView dgvMostrar;
        private System.Windows.Forms.TableLayoutPanel tlpCantTot;
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        private System.Windows.Forms.Button btVolver;

        private SqlDataAdapter adapter;
                
        private DataSet ds;

        private Label lbCantTot;
        private Label lbUnidad;
        private GroupBox gbTot;
        private TableLayoutPanel tlpBuscarMatPrim;
        private TextBox tbBuscarMateriaPrima;
        private Button btBuscarMateriaPrim;
        private int locationCantTot = 0;
        private int locationTlpBuscarMateriaPrima = 0;

        public ConsultaStockActualPorMateriaPrimaPanel(Handler handler)
        {
            this.formatearPanelGeneral(handler);

            try
            {
                InitializeComponent();
                this.formatearTableTitulo("CONSULTA DE STOCK ACTUAL POR MATERIA PRIMA");

                string query = "SELECT DISTINCT mp.ID AS idMateriaPrima,mp.nombre AS Nombre,mp.codigo AS Código,mp.descripcion AS Descripción, mp.unidad As Unidad FROM MateriaPrima AS mp INNER JOIN MateriaPrima_X_Lote AS mpxl ON mp.ID=mpxl.materiaPrima AND mpxl.cantidad>'0'";
                adapter = SQLclass.obtenerAdapter(query);
                if (adapter != null)
                {
                    ds = new DataSet();
                    adapter.Fill(ds);
                    
                    if (ds.Tables[0].Rows.Count <= 0)
                    {
                        MessageBox.Show("NO HAY NINGÚN LOTE CARGADO ", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }else
                    {
                        dgvMostrar.DataSource = ds.Tables[0];
                        if(dgvMostrar.CurrentCell!=null)
                            cargarMatPrimaTotalEnLabel(dgvMostrar[0,dgvMostrar.CurrentCell.RowIndex].Value.ToString(), dgvMostrar[4, dgvMostrar.CurrentCell.RowIndex].Value.ToString());
                    }
                                        
                    formatearDataGrid();
                    formatearTablaBuscarMatPrima();
                    this.formatearTablaCantTot();

                    formatearTablaBotones();

                    this.Controls.Add(dgvMostrar);

                    this.Controls.Add(tlpBuscarMatPrim);

                    this.Controls.Add(gbTot);

                    this.Controls.Add(tlpBotones);

                }

                else
                {

                    MessageBox.Show("NO HAY NINGÚN LOTE CARGADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.handler.backToPrincipal();
                }

            }
            catch (MiException ex)
            {

                CLogger.Append("[ConsultaStockActualPanel] Exception: " + ex, CLogger.ERROR);      //DEBUG

            }
        }

        #region Formato
        private void formatearDataGrid()
        {
            dgvMostrar.AutoGenerateColumns = true;
            dgvMostrar.Size = new Size(this.Size.Width - 100, 400);
            dgvMostrar.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvMostrar.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvMostrar.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dgvMostrar.BorderStyle = BorderStyle.Fixed3D;

            dgvMostrar.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvMostrar.Size.Width), this.InitialRealPointVertical);
        }

        private void formatearTablaBuscarMatPrima()
        {
            tlpBuscarMatPrim.Controls.Add(btBuscarMateriaPrim);
            tlpBuscarMatPrim.Controls.Add(tbBuscarMateriaPrima);

            locationTlpBuscarMateriaPrima = dgvMostrar.Size.Height + this.InitialRealPointVertical + 20;

            tlpBuscarMatPrim.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpBuscarMatPrim.Size.Width), locationTlpBuscarMateriaPrima);



        }

        private void formatearTablaCantTot()
        {
            tlpCantTot.Controls.Add(lbCantTot);
            tlpCantTot.Controls.Add(lbUnidad);

            gbTot.Controls.Add(tlpCantTot);

            //tlpBuscarProv.Size = new Size(lbBuscarProvCuil.Size.Width + tbBuscarProvCuil.Size.Width + 10,tbBuscarProvCuil.Size.Height);
            locationCantTot = locationTlpBuscarMateriaPrima + tbBuscarMateriaPrima.Size.Height + 25;

            int wid = MetodosGenerales.calcularCenterWidth(handler.FormFrame.Size.Width, gbTot.Size.Width);

            gbTot.Location = new Point(wid, locationCantTot);

            tlpCantTot.Location = new Point(MetodosGenerales.calcularCenterWidth(gbTot.Size.Width,tlpCantTot.Size.Width ),30 );

            

        }

        

        private void formatearTablaBotones()
        {
            int locationBtCambios = locationCantTot + gbTot.Size.Height + 15;
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, tlpBotones.Size.Width), locationBtCambios);

            tlpBotones.Controls.Add(btVolver);


        }


        #endregion

        #region INICIALIZAR COMPONETES
        private void InitializeComponent()
        {
            this.dgvMostrar = new System.Windows.Forms.DataGridView();
            this.tlpCantTot = new System.Windows.Forms.TableLayoutPanel();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btVolver = new System.Windows.Forms.Button();
            this.lbCantTot = new System.Windows.Forms.Label();
            this.lbUnidad = new System.Windows.Forms.Label();
            this.gbTot = new System.Windows.Forms.GroupBox();
            this.tlpBuscarMatPrim = new System.Windows.Forms.TableLayoutPanel();
            this.tbBuscarMateriaPrima = new System.Windows.Forms.TextBox();
            this.btBuscarMateriaPrim = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrar)).BeginInit();
            this.SuspendLayout();
            // 
            // dgvMostrar
            // 
            this.dgvMostrar.AllowUserToResizeRows = false;
            this.dgvMostrar.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvMostrar.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnF2;
            this.dgvMostrar.Location = new System.Drawing.Point(0, 0);
            this.dgvMostrar.Name = "dgvMostrar";
            this.dgvMostrar.ReadOnly = true;
            this.dgvMostrar.RowTemplate.DefaultCellStyle.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dgvMostrar.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
            this.dgvMostrar.Size = new System.Drawing.Size(240, 150);
            this.dgvMostrar.TabIndex = 0;
            this.dgvMostrar.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrar_CellClick);
            this.dgvMostrar.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrar_CellContentClick);
            // 
            // tlpCantTot
            // 
            this.tlpCantTot.BackColor = System.Drawing.Color.LightCyan;
            this.tlpCantTot.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpCantTot.ColumnCount = 2;
            this.tlpCantTot.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpCantTot.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpCantTot.Location = new System.Drawing.Point(0, 0);
            this.tlpCantTot.Name = "tlpCantTot";
            this.tlpCantTot.RowCount = 1;
            this.tlpCantTot.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpCantTot.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpCantTot.Size = new System.Drawing.Size(300, 40);
            this.tlpCantTot.TabIndex = 0;
            // 
            // tlpBotones
            // 
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 1;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Size = new System.Drawing.Size(100, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // lbCantTot
            // 
            this.lbCantTot.AutoSize = true;
            this.lbCantTot.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCantTot.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCantTot.Location = new System.Drawing.Point(0, 0);
            this.lbCantTot.Name = "lbCantTot";
            this.lbCantTot.Size = new System.Drawing.Size(100, 23);
            this.lbCantTot.TabIndex = 0;
            this.lbCantTot.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbUnidad
            // 
            this.lbUnidad.AutoSize = true;
            this.lbUnidad.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbUnidad.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbUnidad.Location = new System.Drawing.Point(0, 0);
            this.lbUnidad.Name = "lbUnidad";
            this.lbUnidad.Size = new System.Drawing.Size(100, 23);
            this.lbUnidad.TabIndex = 0;
            this.lbUnidad.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // gbTot
            // 
            this.gbTot.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.gbTot.Location = new System.Drawing.Point(0, 0);
            this.gbTot.Name = "gbTot";
            this.gbTot.Size = new System.Drawing.Size(400, 100);
            this.gbTot.TabIndex = 0;
            this.gbTot.TabStop = false;
            this.gbTot.Text = "Cantidad Total";
            // 
            // tlpBuscarMatPrim
            // 
            this.tlpBuscarMatPrim.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarMatPrim.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarMatPrim.ColumnCount = 2;
            this.tlpBuscarMatPrim.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMatPrim.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMatPrim.Cursor = System.Windows.Forms.Cursors.Default;
            this.tlpBuscarMatPrim.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarMatPrim.Name = "tlpBuscarMatPrim";
            this.tlpBuscarMatPrim.RowCount = 1;
            this.tlpBuscarMatPrim.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMatPrim.Size = new System.Drawing.Size(500, 40);
            this.tlpBuscarMatPrim.TabIndex = 0;
            // 
            // tbBuscarMateriaPrima
            // 
            this.tbBuscarMateriaPrima.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarMateriaPrima.MaxLength = 50;
            this.tbBuscarMateriaPrima.Name = "tbBuscarMateriaPrima";
            this.tbBuscarMateriaPrima.Size = new System.Drawing.Size(200, 30);
            this.tbBuscarMateriaPrima.TabIndex = 0;
            // 
            // btBuscarMateriaPrim
            // 
            this.btBuscarMateriaPrim.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarMateriaPrim.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarMateriaPrim.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarMateriaPrim.Location = new System.Drawing.Point(0, 0);
            this.btBuscarMateriaPrim.Name = "btBuscarMateriaPrim";
            this.btBuscarMateriaPrim.Size = new System.Drawing.Size(75, 23);
            this.btBuscarMateriaPrim.TabIndex = 0;
            this.btBuscarMateriaPrim.Text = "Buscar Materia Prima (Nombre)";
            this.btBuscarMateriaPrim.UseVisualStyleBackColor = false;
            this.btBuscarMateriaPrim.Click += new System.EventHandler(this.btBuscarMateriaPrim_Click);
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrar)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion 

        private void cargarMatPrimaTotalEnLabel(string id,string unidad)
        {
            try
            {
                if (dgvMostrar.CurrentCell == null)
                {
                    return;
                }

                lbCantTot.Text = "";
                lbUnidad.Text = "";

                double cantTotMatPrima = handler.obtenerCantidadTotalMateriaPrima_X_LotePorId(id);


                if (cantTotMatPrima >0)
                {
                    lbCantTot.Text = cantTotMatPrima.ToString("F");
                    lbUnidad.Text = unidad;

                }
                else if (cantTotMatPrima == 0)
                {

                    MessageBox.Show("NO HAY STOCK DE LA MATERIA PRIMA SELECCIONADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    //this.handler.backToPrincipal();
                }
                else
                {

                    MessageBox.Show("NO HAY STOCK DE LA MATERIA PRIMA SELECCIONADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    //this.handler.backToPrincipal();
                }

            }
            catch (MiException ex)
            {

                CLogger.Append("[cargarMatPrimaTotalEnLabel] Exception: " + ex, CLogger.ERROR);      //DEBUG

            }
        }


        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

       
        
        private void dgvMostrar_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMostrar.SelectionMode = DataGridViewSelectionMode.CellSelect;

            if (dgvMostrar.CurrentCell != null)
            {
                cargarMatPrimaTotalEnLabel(dgvMostrar[0, dgvMostrar.CurrentCell.RowIndex].Value.ToString(), dgvMostrar[4, dgvMostrar.CurrentCell.RowIndex].Value.ToString());
            }
        }

        private void dgvMostrar_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMostrar.SelectionMode = DataGridViewSelectionMode.CellSelect;

            if (dgvMostrar.CurrentCell != null)
                cargarMatPrimaTotalEnLabel(dgvMostrar[0, dgvMostrar.CurrentCell.RowIndex].Value.ToString(), dgvMostrar[4, dgvMostrar.CurrentCell.RowIndex].Value.ToString());
        }

        private void btBuscarMateriaPrim_Click(object sender, EventArgs e)
        {
            if (dgvMostrar.CurrentCell == null)
            {
                MessageBox.Show("LA LISTA SE ENCUENTRA VACIA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            dgvMostrar.ClearSelection();
            if (!string.IsNullOrWhiteSpace(tbBuscarMateriaPrima.Text))
            {
                string searchValue = tbBuscarMateriaPrima.Text;
                

                try
                {
                    foreach (DataGridViewRow row in dgvMostrar.Rows)
                    {
                        if (row.Cells[1].Value.ToString().Equals(searchValue))
                        {
                            dgvMostrar.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;

                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvMostrar.CurrentCell = row.Cells[0];
                            tbBuscarMateriaPrima.Clear();
                            
                            cargarMatPrimaTotalEnLabel(dgvMostrar[0, dgvMostrar.CurrentCell.RowIndex].Value.ToString(), dgvMostrar[4, dgvMostrar.CurrentCell.RowIndex].Value.ToString());

                            break;

                        }

                    }
                }
                catch (Exception exc)
                {
                    tbBuscarMateriaPrima.Clear();
                    MessageBox.Show("NO SE ENCONTRO LA MATERIA PRIMA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // CLogger.Append("[btBuscarProducto_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO MATERIA PRIMA A BUSCAR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

    }
}
